/**
 * qq音乐插入弹窗（暂未使用）
 */
define( [ 'jquery', 'require', 'message' ], function( $, require ) {

	var ShowTitleMessage = require('message');
	var showTime = ShowTitleMessage.showTime;

	var me = $( '#insertQQmusic' );
	var qqMusicOpenApi = 'https://auth-external.music.qq.com/open/fcgi-bin/fcg_weixin_music_search.fcg';

	function timeFormat( i ) {
		var e = "";
		if ( 60 > i ) e = "00:" + ( 10 > i ? "0" : "" ) + i;
		else {
			var t = Math.floor( i / 60 ),
				n = i - 60 * t;
			e = ( 10 > t ? "0" : "" ) + t + ":" + ( 10 > n ? "0" : "" ) + n;
		}
		return e;
	}

	function getMusicItemHtmlStr( obj ) {
		var f = obj.f.split( '|' );
		var duration = f[ 7 ];
		var html = '';

		html += '<label class="qqmusic_item">';
		html += '<input type="radio" name="music_radio" class="music_radio" />';
		html += '<span class="lbl_content">';
		html += '<span class="qqmusic_meta qqmusic_thumb_info">';
		html += '<span class="songname">' + obj.songname + '</span>';
		html += '<span class="singername gray">' + obj.singername + '</span>';
		html += '</span>';
		html += '<span class="qqmusic_meta qqmusic_songsize gray">' + ( f[ 12 ] > 1048576 ? parseInt( f[ 12 ] / 1048576 ) + "M" : "1M" ) + '</span>';
		html += '<span class="qqmusic_meta qqmusic_songtime gray">' + timeFormat( duration ) + '</span>';
		html += '<span class="musicInfo" >';
		html += '<span class="glyphicon glyphicon-play-circle icon_qqmusic gray"></span>';
		html += '</span>';
		html += '</span>';
		html += '</label>';

		return $( html ).data( 'thisMusicData', obj );
	}

	function getMusicPreviewHtmlStr( music ) {
		var n = music.f.split( '|' ),
			s = n[ n.length - 1 ],
			mid = n[ n.length - 3 ];
		var albumurl = "/" + s.charAt( s.length - 2 ) + "/" + s.charAt( s.length - 1 ) + "/" + s + ".jpg";

		var html = '';

		html += '<qqmusic class="" scrolling="no" frameborder="0" musicid="' + music.id + '" mid="' + mid + '" albumurl="' + albumurl + '" audiourl="' + music.m4a + '" music_name="' + music.songname + '" commentid="" singer="' + music.singername + '" play_length="240000"></qqmusic>';

		/*html += '<div class="qqmusic_wrp">';
		html += '<span class="glyphicon glyphicon-play-circle icon_qqmusic_switch"></span>';
		html += '<div class="qqmusic_content">';
		html += '<p class="qqmusic_title">' + music.songname + '</p>';
		html += '<p class="qqmusic_desc gray">' + music.songname + '</p>';
		html += '</div>';
		html += '</div>';*/

		return html;
	}

	function doMusicSearch( options, fn ) {

		var searchParam = {
			remoteplace: 'txt.weixin.officialaccount',
			w: '',
			platform: 'weixin',
			perpage: 10,
			curpage: 1,
			jsonCallback: 'MusicJsonCallback'
		}

		$.extend( searchParam, options );
		$( '.spinner', me ).show();
		$.ajax( {
			url: qqMusicOpenApi,
			type: 'get',
			data: searchParam,
			dataType: 'jsonp'
		} );

		window.MusicJsonCallback = function( data ) {
			$( '.spinner', me ).hide();
			if ( !data.retcode ) {
				var totalPage = parseInt( data.totalnum / data.perpage ) || 1;
				if ( data.totalnum % data.perpage ) {
					++totalPage;
				}
				$( '.pageControl .maxPage' ).text( totalPage );
				$( '.pageControl .current_page' ).text( data.curpage );
				if ( totalPage > 1 ) {
					$( '.pageControl .next', me ).removeAttr( 'disabled' );
				}
				$( '.music_detail', me ).empty();

				for ( i in data.list ) {
					var music = data.list[ i ];
					$( '.music_detail', me ).append( getMusicItemHtmlStr( music ) );
				}

			}
		}

	}

	function pageInputCheck() {
		var page = $( this ).val() * 1;
		var max = $( this ).parent().find( '.maxPage' ).text() * 1;
		if ( !page ) {
			$( this ).val( '' );
		} else {
			if ( page > max ) {
				$( this ).val( max );
			}
		}
	}

	me.on( 'click', '.search_btn', function() {
		var val = $.trim( $( this ).prev().val() );
		if ( /^\s*$/.test( val ) ) return;
		doMusicSearch( {
			w: val,
			curpage: 1
		} );
	} ).on( 'click', '#doInsertMusic', function() {
		var musicData = $( '.music_radio:checked', me ).parent().data( 'thisMusicData' );
		console.log( getMusicPreviewHtmlStr( musicData ) )
		editorObj.execCommand( 'inserthtml', getMusicPreviewHtmlStr( musicData ) );
		me.modal( 'hide' );
	} );
	$( '.pageControl', me ).on( 'click', '.goPage', function() {
		var me = $( this );
		var page = me.parent().find( '.inp_page' ).val() * 1;
		var max = $( this ).parent().find( '.maxPage' ).text() * 1;
		var currentPage = $( this ).parent().find( '.current_page' ).text() * 1;
		if ( currentPage === page ) return;
		if ( page ) {
			if ( page >= max ) {
				me.parent().find( '.next' ).prop( 'disabled', 'disabled' );
			} else {
				me.parent().find( '.next' ).removeAttr( 'disabled' );
			}
			if ( page > 1 ) {
				me.parent().find( '.prev' ).removeAttr( 'disabled', 'disabled' );
			} else {
				me.parent().find( '.prev' ).prop( 'disabled' );
			}

		} else {
			$( this ).val( '' );
			ShowTitleMessage.showWarning( '请输入正确的页码', showTime );
		}
	} ).on( {
		change: pageInputCheck,
		keyup: pageInputCheck
	}, '.inp_page' ).on( 'click', '.next', function() {
		var me = $( this );
		var currentObj = articleOptions[ ArticleChooseHtmlBuild.options.tabIndex ];
		var page = ( currentObj.param.p || currentObj.param.page_num ) + 1;
		var max = me.parent().find( '.maxPage' ).text() * 1;
		if ( page >= max ) {
			me.prop( 'disabled', 'disabled' );
		}
		currentObj.setAttrbute( {
			p: page,
			page_num: page
		}, function() {
			me.parent().find( '.prev' ).removeAttr( 'disabled' );
		} );

	} ).on( 'click', '.prev', function() {

		var me = $( this ).parent();
		var currentObj = articleOptions[ ArticleChooseHtmlBuild.options.tabIndex ];
		var page = ( currentObj.param.p || currentObj.param.page_num ) - 1;
		if ( page <= 1 ) {
			$( this ).prop( 'disabled', 'disabled' );
		}
		currentObj.setAttrbute( {
			p: page,
			page_num: page
		}, function() {
			me.find( '.next' ).removeAttr( 'disabled' );
		} );
	} );
} );